package com.tanxi521.com.thenorthtitle.util;

import com.tanxi521.com.thenorthtitle.TheNorthTitle;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.RegisteredServiceProvider;

public class VaultApi {

    public static Economy econ = null;

    public static boolean setupEconomy() {
        RegisteredServiceProvider<Economy> economyProvider = TheNorthTitle.instance.getServer().getServicesManager()
                .getRegistration(net.milkbowl.vault.economy.Economy.class);
        if (economyProvider != null) {
            return (econ = economyProvider.getProvider()) != null;
        }
        return false;
    }

    public static double getMoney(OfflinePlayer player) {
        if (econ == null) {
            throw new IllegalStateException("API -> null");
        } else {
            return econ.getBalance(player);
        }
    }

    public static boolean hasMoney(OfflinePlayer player, double minimum) {
        if (econ == null) {
            throw new IllegalStateException("API -> null");
        } else if (minimum < 0.0D) {
            throw new IllegalArgumentException("无效的金额: -> " + minimum);
        } else {
            double balance = econ.getBalance(player);
            return balance >= minimum;
        }
    }

    public static boolean takeMoney(OfflinePlayer player, double amount) {
        if (econ == null) {
            throw new IllegalStateException("API -> null");
        } else if (amount < 0.0D) {
            throw new IllegalArgumentException("无效的金额: -> " + amount);
        } else {
            EconomyResponse response = econ.withdrawPlayer(player, amount);
            return response.transactionSuccess();
        }
    }

    public static boolean giveMoney(OfflinePlayer player, double amount) {
        if (econ == null) {
            throw new IllegalStateException("API -> null");
        } else if (amount < 0.0D) {
            throw new IllegalArgumentException("无效的金额: -> " + amount);
        } else {
            EconomyResponse response = econ.depositPlayer(player, amount);
            return response.transactionSuccess();
        }
    }

}